//订单列表
var urlOrderList = server_host + '/userweb/getUserOrderList.do';
//餐点列表
var urlMealList = server_host + '/userweb/wxfood/getWXOrderFoodInfoList.do';

var _scope = null;

var appModule = angular.module('CheerHiWx', []);
appModule.controller('OrderCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout) {

	_scope = angular.element('body').scope();

	//包厢订单当前页
	$scope.pageIndexOrder = 1;
	//餐点订单当前页
	$scope.pageIndexMeal = 1;

	//列表类型，0包厢，1餐点
	$scope.listType = 1;

	//包厢订单状态
	$scope.orderStatus = [{
		code: 1,
		dscp: '订单未付款，未消费',
		refundBtn: true,
		payBtn: true
	}, {
		code: 2,
		dscp: '订单未付款，已消费',
		refundBtn: false,
		payBtn: true
	}, {
		code: 3,
		dscp: '订单已付款，未消费',
		refundBtn: true,
		payBtn: false
	}, {
		code: 4,
		dscp: '订单已付款，已消费',
		refundBtn: false,
		payBtn: false
	}, {
		code: 5,
		dscp: '退款中',
		refundBtn: false,
		payBtn: false
	}, {
		code: 6,
		dscp: '已退订',
		refundBtn: false,
		payBtn: false
	}, {
		code: 7,
		dscp: '已退款',
		refundBtn: false,
		payBtn: false
	}, {
		code: 8,
		dscp: '现场未付款,未消费',
		refundBtn: true,
		payBtn: false
	}, {
		code: 9,
		dscp: '现场未付款,已消费',
		refundBtn: false,
		payBtn: false
	}, {
		code: 10,
		dscp: '已现场付款,已消费',
		refundBtn: false,
		payBtn: false
	}];

	$scope.nextOperate = [{
		code: 1,
		dscp: '去点餐'
	}, {
		code: 2,
		dscp: '去加餐'
	}, {
		code: 3,
		dscp: '去评价'
	}, {
		code: 4,
		dscp: '已评价'
	}, {
		code: 0,
		dscp: '订单取消'
	}];

	//餐点状态
	$scope.mealStatus = [{
		code: 1,
		dscp: '未支付'
	}, {
		code: 2,
		dscp: '已支付'
	}, {
		code: 3,
		dscp: '申请退款中'
	}, {
		code: 4,
		dscp: '退款中'
	}, {
		code: 5,
		dscp: '拒绝退款'
	}, {
		code: 6,
		dscp: '已退款'
	}];

	//餐点送达状态
	$scope.mealArriveStatus = [{
		code: 0,
		dscp: '未送达'
	}, {
		code: 1,
		dscp: '已送达'
	}];

	$scope.findElement = findElement;

	$scope.swtichType = function(typeIndex) {
		$scope.listType = typeIndex;
	}

	//加载列表
	$scope.loadList = function(loadedCallback) {
		var URL = '';
		if ($scope.listType == 0) {
			URL = urlOrderList + '?page=' + $scope.pageIndexOrder;
		} else {
			URL = urlMealList + '?pageIndex=' + $scope.pageIndexMeal;
		}
		$http.get(URL).success(function(data, status, headers, config) {
			console.log(data);
			if ($scope.listType == 0)
				$scope.orderList = data.data.userOrderTableList;
			else
				$scope.mealList = data.data.userOrderFoodList;
			angular.element('.pull-up').html('加载更多……');

			if (typeof loadedCallback == 'function') {
				loadedCallback();
			}
		});
	}

	//加载更多
	$scope.loadMore = function() {
		var URL = '';
		if ($scope.listType == 0) {
			$scope.pageIndexOrder++;
			URL = urlOrderList + '?page=' + $scope.pageIndexOrder;
		} else {
			$scope.pageIndexMeal++;
			URL = urlMealList + '?pageIndex=' + $scope.pageIndexMeal;
		}
		$http.get(URL).success(function(data, status, headers, config) {
			if ($scope.listType == 0) {
				orderAlreadyloading = false;
				if (data.data.userOrderTableList.length > 0) {
					$scope.orderList = $scope.orderList.concat(data.data.userOrderTableList);
				} else {
					angular.element('.pull-up').html('到底儿了~');
				}
			} else {
				mealAlreadyloading = false;
				if (data.data.userOrderFoodList.length > 0) {
					$scope.mealList = $scope.mealList.concat(data.data.userOrderFoodList);
				} else {
					angular.element('.pull-up').html('到底儿了~');
				}
			}
		});
	}

	//订单详情
	$scope.toDetail = function(orderNum,stateCode) {
		location.href = 'order-details.html?orderNum=' + orderNum + '&stateCode=' + stateCode;
	}

	//餐点订单详情
	$scope.toMealDetail = function(orderNum,stateCode) {
		location.href = 'orderMealDetail.html?orderNum=' + orderNum + '&stateCode=' + stateCode;
	}

	$scope.loadList(function() {
		$scope.listType = 0;
		$scope.loadList();
	});

	// 去点餐
	$scope.toOrder = function($event, shopName, shopId, orderNum) {
		$event.stopPropagation();
		sessionStorage.setItem('shopId', shopId);
		sessionStorage.setItem('shopName', shopName);
		location.href = 'order-foodinfo.html?orderNum=' + orderNum + '&fr=order';
	}

	//去评价
	$scope.goRate = function($event, orderNum) {
		$event.stopPropagation();
		location.href = 'rate.html?orderNum=' + orderNum;
	}

}]);

var orderAlreadyloading = false;
var mealAlreadyloading = false;

$('.orderlist').scroll(function() {
	if ($('.scroll-container').height() <= ($('.orderlist').height() + $('.orderlist').scrollTop())) {
		if (!orderAlreadyloading) {
			orderAlreadyloading = true;
			_scope.loadMore();
		}

	}
});

$('.meallist').scroll(function() {
	if ($('.scroll-container').height() <= ($('.orderlist').height() + $('.orderlist').scrollTop())) {
		if (!mealAlreadyloading) {
			mealAlreadyloading = true;
			_scope.loadMore();
		}
	}
});